package com.aaa.power.util;

import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;


/**
 * @fileName:GlobalExceptionHander
 * @description:
 * @author:忆尘
 * @createTime:2020/11/9 20:51
 * @version:1.0.0
 */

//所有controller的请求方法，都会走
//@RestControllerAdvice
public class GlobalExceptionHandler {


/**
     * 拦截到所有的异常，
     * @param ex
     * @return
     */

    @ExceptionHandler(value = Exception.class)
    public Result ExceptionHandler(Exception ex){
        //判断异常是不是自定义异常
        if(ex instanceof CustomException){
            return new Result(((CustomException) ex).getErrorCode(),((CustomException) ex).getErrorMsg(),"自定义异常");
        }
        //系统异常
        return new Result(ReturnCode.FAIL.getCode(), ReturnCode.FAIL.getMsg(),ex.getClass().getName());
    }

}
